<?php
/**
*
* @package phpbbtools
* @version $Id: phpbbtools.php,v 1.70 2007/11/08 14:36:34 mikov Exp $
* @copyright (c) 2007 phpbbchina.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

defined('_JEXEC') or die();

require(dirname(__FILE__) . '/include_functions.php');
require(dirname(__FILE__) . '/utf_tools.php');
/**
 * Authentication plugin
 *
 * @author Mikov Chain <mikovchain@gmail.com>
 * @package		utf
 * @subpackage	phpbbtools
 * @since 1.0
 */
class JToolsPhpBB
{
	function &getPhpBBConfig()
	{
		static $instance;

		if (!is_object($instance))
		{
			require_once dirname(__FILE__) . '/configuration.php';
			$instance = new JConfigPhpBB;
		}

		return $instance;
	}

	function &getPhpBBDBO()
	{
		static $instance; 

		if (!is_object( $instance )) {
			$c = &JToolsPhpBB::getPhpBBConfig();
			require(dirname(__FILE__) . '/db/' . $c->dbms . '.php');
			$sql_db = (!empty($c->dbms)) ? 'dbal_' . basename($c->dbms) : 'dbal';

			$instance = new $sql_db();

			// Connect to DB
			$instance->sql_connect($c->dbhost, $c->dbuser, $c->dbpasswd, $c->dbname, $c->dbport, false, true);

			// We do not need this any longer, unset for safety purposes
			unset($dbpasswd);
		}
		return $instance;
	}

	function phpbb3_user_login($user_data) 
	{
		if(!defined('IN_PHPBB')) {
			$c = &JToolsPhpBB::getPhpBBConfig();
			
			if ($user_data['user_id'] == -1){
				return -1;
			}
			$session_key = md5(unique_id());
			$phpbb_cookie_name = JToolsPhpBB::phpbb3_read_config('cookie_name'); 
			$phpbb_cookie_domain = JToolsPhpBB::phpbb3_read_config('cookie_domain');
			JToolsPhpBB::phpbb3_insert_session($user_data, $session_key);
			JToolsPhpBB::phpbb3_set_cookies($session_key, $phpbb_cookie_name, $phpbb_cookie_domain);
			
			$db =& JToolsPhpBB::getPhpBBDBO();
		
			$query = 'UPDATE `' . $c->prefix . 'users` SET 
				user_lastvisit = '. time() .' 
				WHERE 
				user_id = '. $user_data['user_id'];

			//echo $query;exit;
			$db->sql_query( $query );
		}
	}

	// Get config value.
	function phpbb3_read_config($config_name) 
	{
		static $instance;

		if (!is_object($instance))
		{
			$c = &JToolsPhpBB::getPhpBBConfig();
			$db =& JToolsPhpBB::getPhpBBDBO();

			$query = 'SELECT `config_name`, `config_value` 
				FROM `' . $c->prefix . 'config`';
	
			//echo $query;//exit;
			$result = $db->sql_query($sql);
			while ($row = $db->sql_fetchrow($result))
			{
				$instance['config_name'] = $row['config_name'];
			}
			$db->sql_freeresult($result);
		}
		if (isset($instance[$config_name])){
			return $instance[$config_name]['config_value'];
		} else {
			return 'phpbb3_xonze';
		}
	}
	
	// Set config value.
	function phpbb3_set_config($config_name, $config_value)
	{
		$c = &JToolsPhpBB::getPhpBBConfig();
		$db =& JToolsPhpBB::getPhpBBDBO();
		$query = 'UPDATE `' . $c->prefix . 'config` 
			SET `config_value`=\'' . $config_value . '\'  
			WHERE config_name=\'' . $config_name .'\'';

		$db->sql_query( $query );
	}
	
	function phpbb3_get_userdata($user_name) 
	{
		$c = &JToolsPhpBB::getPhpBBConfig();
		$db =& JToolsPhpBB::getPhpBBDBO();
		
		$sql = 'SELECT `user_id` AS ID, 
			`username` AS user_login, 
			`username` AS display_name,
			`username_clean` AS user_nicename, 
			`user_password` AS user_pass, 
			`user_email`, 
			`user_website` AS user_url, 
			`user_regdate` AS user_registered 
			FROM `' . $c->prefix . 'users` 
			WHERE username_clean=\'' . utf8_clean_string($user_name) . '\'';

		//echo $query;exit;
		$result = $db->sql_query($sql);
		$user_data = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		return $user_data;
	}
	
	function phpbb3_get_userid($user_name) 
	{
		$c = &JToolsPhpBB::getPhpBBConfig();
		$db =& JToolsPhpBB::getPhpBBDBO();
		
		$query = 'SELECT `user_id` FROM `' . $c->prefix . 'users` WHERE 
			username_clean=\'' . utf8_clean_string($user_name) . '\'';

		//echo $query;exit;
		$result = $db->sql_query($sql);
		$user_id = (int) $db->sql_fetchfield('user_id');
		$db->sql_freeresult($result);

		return $user_id;
	}

	// Session management.
	function phpbb3_insert_session($user_data, $session_key) 
	{
		$c = &JToolsPhpBB::getPhpBBConfig();
		$db =& JToolsPhpBB::getPhpBBDBO();

		$query = 'INSERT INTO `' . $c->prefix . 'sessions` 
			(`session_id`, 
			`session_user_id`, 
			`session_last_visit`,
			`session_start`, 
			`session_time`, 
			`session_ip`, 
			`session_browser`, 
			`session_page`, 
			`session_autologin`, 
			`session_admin`) 
			VALUES 
			(\''. substr($session_key, 0, 32) .'\', \''. 
			$user_data['user_id'] .'\', '. 
			$user_data['user_lastvisit'] .', '. 
			time() .', '. 
			time() .', \''. 	
			$_SERVER['REMOTE_ADDR'] .'\', \''. 
			$_SERVER['HTTP_USER_AGENT'] .'\', 
			0,
			0,
			0)';

		//echo $query;exit;
		$db->sql_query( $query );
	}

	// cookie management.
	function phpbb3_set_cookies($session_key, $phpbb_cookie_name, $phpbb_cookie_domain) 
	{
		phpbb_set_cookie('k', '', time() - 6000, $phpbb_cookie_name, $phpbb_cookie_domain);
		phpbb_set_cookie('u', '', time() - 6000, $phpbb_cookie_name, $phpbb_cookie_domain);
		phpbb_set_cookie('sid', $session_key, time() + 31536000, $phpbb_cookie_name, $phpbb_cookie_domain);
	}

	// Logout phpbb3.
	function phpbb3_user_logout($user_data) 
	{
		if(!defined('IN_PHPBB')) {
			if ($user_id == -1){
				return -1;
			}
			$phpbb_cookie_name = JToolsPhpBB::phpbb3_read_config('cookie_name');
			$phpbb_cookie_domain = JToolsPhpBB::phpbb3_read_config('cookie_domain');
			JToolsPhpBB::phpbb3_delete_session($user_data['user_id']);
			JToolsPhpBB::phpbb3_unset_cookies($phpbb_cookie_name, $phpbb_cookie_domain);
		}
	}

	// destroy session.
	function phpbb3_delete_session($user_data) 
	{
		$c = &JToolsPhpBB::getPhpBBConfig();
		$db =& JToolsPhpBB::getPhpBBDBO();
		
		$query = 'DELETE FROM `' . $c->prefix . 'sessions` WHERE 
			session_user_id = '. $user_data['user_id'] .' AND session_ip =\''. phpbb3_encoded_ip() .'\'';

		//echo $query;exit;
		$db->sql_query( $query );
	}

	// destroy cookie.
	function phpbb3_unset_cookies($phpbb_cookie_name, $phpbb_cookie_domain) 
	{
		phpbb_set_cookie('k', '', time() - 6000, $phpbb_cookie_name, $phpbb_cookie_domain);
		phpbb_set_cookie('u', '', time() - 6000, $phpbb_cookie_name, $phpbb_cookie_domain);
		phpbb_set_cookie('sid', '', time() - 6000, $phpbb_cookie_name, $phpbb_cookie_domain);
	}
}

?>